package com.jams.goodox.activity.mine.buy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.qa.entity.CourseComment;
import com.jams.goodox.activity.qa.entity.CourseSeriousId;
import com.jams.goodox.adapter.lfadapter.CommonRecycleViewAdapter;
import com.jams.goodox.adapter.lfadapter.LFRecyclerViewHolder;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.TimeUtils;
import com.jams.goodox.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/5/21.
 */

public class RecordFirstStepActivity extends BaseActivity implements
        OnItemClickListener,LFRecyclerView.LFRecyclerViewListener,
        LFRecyclerView.LFRecyclerViewScrollChange  {

    private static final String TAG = "RecordFirstStepActivity";

    @Bind(R.id.pull_refresh_list)
    LFRecyclerView recycleView;

    @Bind(R.id.creatnew)
    TextView creatnew;

    View headerView;
    private CommonRecycleViewAdapter pAdapter;
    private List<CourseSeriousId> dataList = new ArrayList<>();

    Bundle bundle;

    @Override
    protected int getLayoutId() {
        return R.layout.record_1step_activity;
    }

    @Override
    protected void init() {
        bundle = this.getIntent().getExtras(); //读取intent的数据给bundle对象
//        final String filePath = bundle.getString("filePath");
//        final String title= bundle.getString("title");
//        final int time=bundle.getInt("time",0);

        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        StyleUtil.titleBackKeyWithRightTextViewListener(this, "课程信息完善", "下一步", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //qStartActivityForResult(RecordNextStepActivity.class,bundle,101);
//                Intent  intent = new Intent(RecordFirstStepActivity.this,RecordNextStepActivity.class);
//                intent.putExtra("filePath",filePath);
//                intent.putExtra("title",title);
//                intent.putExtra("time",time);
//                startActivityForResult(intent,101);

                qStartActivityForResult(RecordNextStepActivity.class,bundle,101);
            }
        });


        creatnew .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qStartActivity(NewSerialActivity.class);
            }
        });

        headerView = getLayoutInflater().inflate(R.layout.record_1step_header,null);

        recycleView.setLoadMore(false);
        recycleView.setRefresh(false);
        //recycleView.setNoDateShow();
        recycleView.setAutoLoadMore(false);
        recycleView.setOnItemClickListener(this);
        recycleView.setLFRecyclerViewListener(this);
        recycleView.setScrollChangeListener(this);
        recycleView.setFootText("");

        pAdapter = new CommonRecycleViewAdapter(recycleView, dataList, R.layout.homefragment_text) {
            @Override
            public void bindConvert(LFRecyclerViewHolder holder, int position, Object item, boolean isScrolling) {

                CourseSeriousId pinLun = (CourseSeriousId) item;
                holder.setText(R.id.title, pinLun.getName());
                holder.setTextSize(R.id.title,14);
            }
        };

        recycleView.setHeaderView(headerView);
        recycleView.setAdapter(pAdapter);
    }


    @Override
    protected void onResume() {
        super.onResume();
        dataList.clear();
        getCourseSerious();
    }

    private void getCourseSerious(){
        clearMap();

        params.put("requestData", "{memberId:" + SePref.getId() + "}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);

        showLoading();
        Call<ResponseBody> example = createCallService(LoginService.class).getCourseSerious(body);

        example.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String data = response.body().string();
                    Log.d(TAG, "getCourseSerious = " + data);
                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code")){

                        JSONArray jsonArray = new JSONArray(jsonObject.getString("responseData"));
                        if(null == jsonArray || jsonArray.length() <= 0){
                            return;
                        }

                        for (int i = 0; i < jsonArray.length(); i ++){
                            JSONObject object = jsonArray.getJSONObject(i);
                            CourseSeriousId courseSeriousId = new CourseSeriousId();
                            courseSeriousId.setUpdateId(object.getInt("updateId"));
                            courseSeriousId.setCreateId(object.getInt("createId"));
                            courseSeriousId.setName(object.getString("name"));
                            courseSeriousId.setId(object.getInt("id"));
                            courseSeriousId.setStatus(object.getInt("status"));

                            courseSeriousId.setCreateTime(object.getString("createTime"));
                            courseSeriousId.setUpdateTime(object.getString("updateTime"));

                            dataList.add(courseSeriousId);
                        }

                        pAdapter.notifyDataSetChanged();

                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }

                    misLoading();
                    pAdapter.notifyDataSetChanged();

                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                    misLoading();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d(TAG,"onFailure = " + t.getMessage());
            }
        });
    }

    @Override
    public void onRecyclerViewScrollChange(View view, int i, int i1) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(int position) {
        Log.d(TAG,"------------------------" + position);
        CourseSeriousId courseSeriousId = dataList.get(position);
        qStartActivityForResult(RecordNextStepActivity.class,bundle,101);
    }

    @Override
    public void onLongClick(int po) {

    }
}
